import logging

from rest_framework import status
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet
from rest_framework.permissions import IsAuthenticated
from django_redis import get_redis_connection

from course.models import Course, CourseExpire
from EDU.settings.constants import IMG_SRC

log = logging.getLogger('django')


class CartViewSet(ViewSet):

    permission_classes = [IsAuthenticated]  #认证

    def add_cart(self, request):
        print(1)
        course_id = request.data.get('course_id')
        print(9, course_id)
        user_id = request.user.id
        print(8, user_id)
        select = True
        expire = 0
        # 校验前端传递的参数
        try:
            Course.objects.get(is_show=True, is_delete=False, id=course_id)
        except Course.DoesNotExist:
            return Response({"message": "您添加课程不存在"}, status=status.HTTP_400_BAD_REQUEST)

        try:
            # 获取redis链接
            redis_connection = get_redis_connection("cart")
            # 使用管道操作
            pipeline = redis_connection.pipeline()
            # 开启管道
            pipeline.multi()
            pipeline.hset("cart_%s" % user_id, course_id, expire)
            pipeline.sadd("selected_%s" % user_id, course_id)
            pipeline.execute()
            # 获取商品总数
            course_len = redis_connection.hlen("cart_%s" % user_id)
        except:
            log.error("购物储存数据失败")
            return Response({"message": "参数有误,添加购物车失败"},
                            status=status.HTTP_507_INSUFFICIENT_STORAGE)

        return Response({"message": "添加课程成功", "cart_length": course_len},
                        status=status.HTTP_200_OK)

    # 展示购物车
    def list_cart(self, request):
        user_id = request.user.id
        redis_connection = get_redis_connection("cart")
        cart_list_bytes = redis_connection.hgetall('cart_%s' % user_id)
        select_list_bytes = redis_connection.smembers('selected_%s' % user_id)
        # 循环从mysql中查询商品信息
        data = []
        for course_id_byte, expire_id_byte in cart_list_bytes.items():
            course_id = int(course_id_byte)
            expire_id = int(expire_id_byte)
            try:
                course = Course.objects.get(is_show=True, is_delete=False, pk=course_id)
            except Course.DoesNotExist:
                continue
            final_price = course.final_price(expire_id)
                # 将购物车所需的信息返回
            data.append({
                "selected": True if course_id_byte in select_list_bytes else False,
                "course_img": IMG_SRC + course.course_img.url,
                "name": course.name,
                "id": course.id,
                # "id": course.id,
                "price": final_price,
                "expire_id": expire_id,
                "expire_list": course.expire_list,
                "final_price": final_price,
            })
        return Response(data)


    # 改变勾选状态
    def change_select(self, request):
        selected = request.data.get('selected')
        course_id = request.data.get('course_id')
        user_id = request.user.id

        redis_connection = get_redis_connection("cart")
        if selected:
            redis_connection.sadd("selected_%s" % user_id, course_id)
        else:
            redis_connection.srem("selected_%s" % user_id, course_id)
        return Response(1)
    # 删除数据
    def delete_cart(self, request):
        course_id = request.data.get('course_id')
        user_id = request.user.id
        print(987, course_id)
        print(789, user_id)
        redis_connection = get_redis_connection("cart")
        redis_connection.srem("selected_%s" % user_id, course_id)
        redis_connection.hdel("cart_%s" % user_id, course_id)
        return Response(1)
    # 修改有效期
    def Modify_validity_period(self,request):
        course_id = request.data.get('course_id')
        value = request.data.get('value')
        user_id = request.user.id
        redis_connection = get_redis_connection("cart")
        redis_connection.hset("cart_%s" % user_id, course_id,value)
        return Response(1)
    # 获取支付页面的数据
    def get_select_course(self, request):
        # 获取购物车中已勾选的商品
        user_id = request.user.id
        redis_connection = get_redis_connection("cart")
        # 获取当前已登录的购物车中的所有商品
        cart_list = redis_connection.hgetall("cart_%s" % user_id)
        select_list = redis_connection.smembers("selected_%s" % user_id)
        print(3333,cart_list)
        total_price = 0
        data = []
        for course_id_byte, expire_id_byte in cart_list.items():
            course_id = int(course_id_byte)
            expire_id = int(expire_id_byte)
            print(22222, course_id)

            if course_id_byte in select_list:
                try:
                    # 获取到购物车中所有的课程信息
                    course = Course.objects.get(is_show=True, is_delete=False, pk=course_id)
                except Course.DoesNotExist:
                    continue
                # 如果有效期的id大于0，则需要通过有效期对应的价格来计算活动真实价  id不大于0则使用课程本身的原价
                original_price = course.price
                expire_text = "永久有效"
                try:
                    if expire_id > 0:
                        course_expire = CourseExpire.objects.get(id=expire_id)
                        # 对应有效期的价格
                        original_price = course_expire.price
                        expire_text = course_expire.expire_text
                except CourseExpire.DoesNotExist:
                    pass
                # 根据已勾选的商品对应的有效期的价格来计算商品的最终价格
                print(99999,expire_id)
                final_price = course.final_price(expire_id)
                print(101010,final_price)
                # 将购物车所需的信息返回
                data.append({
                    "selected": True if course_id_byte in select_list else False,
                    "course_img": IMG_SRC + course.course_img.url,
                    "name": course.name,
                    "id": course.id,
                    "price": original_price,  #原价
                    "expire_id": expire_id,
                    "expire_text": expire_text,
                    "final_price": final_price  # 实际价格
                })

            # 商品叠加后的真实总价
            total_price += float(final_price)

        return Response({"course_list": data, "real_price": total_price, "message": "获取成功"})
